package cn.mothin.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mothin.bean.easyui.DataContent;
import cn.mothin.dao.FunctionModuleDao;
import cn.mothin.dao.MenuConfigDao;
import cn.mothin.dao.MenuItemConfigDao;
import cn.mothin.dao.MobileMenuConfigDao;
import cn.mothin.dao.MobileMenuItemConfigDao;
import cn.mothin.dao.SystemUserDao;
import cn.mothin.entity.FunctionModule;
import cn.mothin.entity.MenuConfig;
import cn.mothin.entity.MenuItemConfig;
import cn.mothin.frame.core.base.BaseService;
import cn.mothin.frame.core.interfaces.system.ILoginLogHelper;

@Service("540016d17b0bf81ac00a9818")
public class MainService extends BaseService {
    @Autowired
    private MenuConfigDao menuConfigDao;
    @Autowired
    private MenuItemConfigDao menuItemConfigDao;
    @Autowired
    private FunctionModuleDao functionModuleDao;
    @Autowired
    private SystemUserDao systemUserDao;
    @Autowired
    private ILoginLogHelper loginLogHelper;
    @Autowired
    private MobileMenuConfigDao mobileMenuConfigDao;
    @Autowired
    private MobileMenuItemConfigDao mobileMenuItemConfigDao;

    /**
     * 登录用户权限查看
     * 
     * @param systemRoleForm
     */
    public List<DataContent> queryLoginUserPermission() {
        if (getLoginUser() == null) {
            return null;
        }
        List<DataContent> dataContentList = new ArrayList<DataContent>();
        List<MenuConfig> menuConfigs = menuConfigDao.findMenuConfigs(null);
        if (menuConfigs.size() == 0) {
            return null;
        }
        for (int i = 0; i < menuConfigs.size(); i++) {
            MenuConfig menuConfig = menuConfigs.get(i);
            DataContent dataContent = new DataContent();
            dataContent.setIcon(menuConfig.getIco());
            dataContent.setIconCls(menuConfig.getIco());
            dataContent.setOrdinal(menuConfig.getOrdinal());
            dataContent.setId(menuConfig.getId());
            dataContent.setText(menuConfig.getName());
            queryMenuSub(dataContent);
            if (dataContent.getChildren() != null) {
                dataContentList.add(dataContent);
            }
        }
        /*
         * if (getLoginUser().isSuperUser()) { List<FunctionModule> list =
         * functionModuleDao.findMenusNotConfig(); if (list.size() > 0) {
         * List<DataContent> ds = new ArrayList<DataContent>();
         * 
         * DataContent dataContent = new DataContent();
         * dataContent.setText("尚未配置"); for (int i = 0; i < list.size(); i++) {
         * FunctionModule functionModule = list.get(i); DataContent d = new
         * DataContent();
         * d.setIconCls(IconClassUtil.makeIconClass(functionModule .getIco()));
         * d.setId(functionModule.getId()); d.setText(functionModule.getName());
         * d.getAttributes().put("url", functionModule.getAction()); ds.add(d);
         * } dataContent.setChildren(ds); dataContentList.add(dataContent); } }
         */

        return dataContentList;
    }

    private void addChild(List<DataContent> permissionTreeDataList,DataContent dataContent){
        for(int i=0;i<permissionTreeDataList.size();i++){
            if(dataContent.getOrdinal()<permissionTreeDataList.get(i).getOrdinal()){
                permissionTreeDataList.add(i, dataContent);
                return;
            }
        }
        permissionTreeDataList.add(dataContent);
    }
    
    private void queryMenuSub(DataContent parent) {
        List<DataContent> permissionTreeDataList = new ArrayList<DataContent>();
        List<MenuConfig> menuConfigs = menuConfigDao.findMenuConfigs(parent
                .getId());
        if (menuConfigs.size() > 0) {
            for (int i = 0; i < menuConfigs.size(); i++) {
                MenuConfig menuConfig = menuConfigs.get(i);
                DataContent dataContent = new DataContent();
                dataContent.setStateClosed(true);
                dataContent.setIcon(menuConfig.getIco());
                dataContent.setIconCls(menuConfig.getIco());
                dataContent.setOrdinal(menuConfig.getOrdinal());
                dataContent.setId(menuConfig.getId());
                dataContent.setText(menuConfig.getName());
                queryMenuSub(dataContent);

                if (dataContent.getChildren() != null) {
                    permissionTreeDataList.add(dataContent);
                }
            }
        }

        List<MenuItemConfig> menuItemConfigList = null;
        if (getLoginUser().isSuperUser()) {
            menuItemConfigList = menuItemConfigDao.findLoginUserSuper(parent
                    .getId());
        } else {
            menuItemConfigList = menuItemConfigDao.findLoginUser(
                    parent.getId(), getLoginUser().getId());
        }

        if (menuItemConfigList.size() > 0) {
            for (int i = 0; i < menuItemConfigList.size(); i++) {
                MenuItemConfig menuItemConfig = menuItemConfigList.get(i);
                FunctionModule functionModule = functionModuleDao
                        .get(menuItemConfig.getFunctionModuleId());
                DataContent dataContent = new DataContent();
                dataContent.setIcon(functionModule.getIco());
                dataContent.setIconCls(functionModule.getIco());
                dataContent.setOrdinal(menuItemConfig.getOrdinal());
                dataContent.setId(functionModule.getId());
                dataContent.setText(functionModule.getName());
                dataContent.getAttributes().put("url",
                        functionModule.getAction());
                //permissionTreeDataList.add(dataContent);
                addChild(permissionTreeDataList,dataContent);
            }
        }

        if (permissionTreeDataList.size() > 0) {
            parent.setChildren(permissionTreeDataList);
        }
    }
}
